package vehicle;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import dao.DBConnection;

public abstract class Vehicle {
    protected String licensePlate; // 车牌号
    public String brand; // 品牌
    protected boolean isRented; // 是否被租
    protected int rentDays; // 租车天数

    public Vehicle(String licensePlate, String brand) {
        this.licensePlate = licensePlate;
        this.brand = brand;
        this.isRented = false;
        this.rentDays = 0;
    }

    public String getLicensePlate() {
        return licensePlate;
    }

    public String getBrand() {
        return brand;
    }

    public boolean isRented() {
        return isRented;
    }

    public int getRentDays() {
        return rentDays;
    }

    public void rent() {
        isRented = true;
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement("UPDATE vehicles SET 租赁情况 = 1 WHERE 车牌号 = ?")) {
            pstmt.setString(1, licensePlate);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void returnCar() {
        isRented = false;
        rentDays = 0;
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement("UPDATE vehicles SET 租赁情况 = 0, 租赁天数 = 0 WHERE 车牌号 = ?")) {
            pstmt.setString(1, licensePlate);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 租车天数的方法
    public void setRentDays(int days) {
        this.rentDays = days;
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement("UPDATE vehicles SET 租车天数 = ? WHERE 车牌号 = ?")) {
            pstmt.setInt(1, days);
            pstmt.setString(2, licensePlate);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static Vehicle getVehicleByLicensePlate(String licensePlate) {
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM vehicles WHERE 车牌号 = ?")) {
            pstmt.setString(1, licensePlate);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                String brand = rs.getString("brand");
                boolean isRented = rs.getBoolean("is_rented");
                int rentDays = rs.getInt("rent_days");
                String type = rs.getString("type");
                Vehicle vehicle = null;
                switch (type) {
                    case "Car":
                        vehicle = new Car(licensePlate, brand);
                        break;
                    case "Truck":
                        vehicle = new Truck(licensePlate, brand);
                        break;
                    case "Bus":
                        vehicle = new Bus(licensePlate, brand);
                        break;
                }
                vehicle.isRented = isRented;
                vehicle.rentDays = rentDays;
                return vehicle;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}